//
//  feedBackVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/11.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
//import

class feedBackVC: LDBaseCustomizeVC {
    
    var titleArr:Array<String> = ["系统消息","意见反馈","常见问题"]
    var systemMessageBtn:UIButton?
    var feedBackBtn:UIButton?
    var commentQuest:UIButton?
    var gampView:UIView?
    
    var line1:UIView?
    var line2:UIView?
    let BtnW = LDFrame.ScreenWidth/3
    let BtnH:CGFloat = 40
    
    var childVC = [Int:AnyObject?]()//:Array<AnyObject>?
    
    let btnsTag:Int = 888
    
    var contentView:UIScrollView?
    var selectTitleButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setDefalutContent()
        setupContentVC()
    }
    
    override func viewWillAppear(animated: Bool) {
        
        rdv_tabBarController.setTabBarHidden(true, animated: true)
    }
    
    lazy var customerConversationVC:meSettingFeedBackChat = {
        
        let vc2 = meSettingFeedBackChat.init(conversationType: .ConversationType_CUSTOMERSERVICE, targetId:LDCustomerTatgetID)
        return vc2
        
    }()
    
    func setDefalutContent() {
        
        gampView = UIView.init()
        gampView?.backgroundColor = LDGolbalColor.White
        
        contentView = UIScrollView.init(frame: view.bounds)//(frame:CGRectMake(0, 0, LDFrame.ScreenWidth, LDFrame.ScreenHeight - LDFrame.NavH))
        contentView?.contentInset = UIEdgeInsetsMake(-64, 0, -0, 0)
        contentView?.contentSize = CGSizeMake(LDFrame.ScreenWidth * CGFloat(titleArr.count), LDFrame.ScreenHeight - LDFrame.NavH - BtnH)
        contentView?.translatesAutoresizingMaskIntoConstraints = false
        contentView?.pagingEnabled = true
        contentView?.showsHorizontalScrollIndicator = false
        contentView?.delegate = self
        contentView?.backgroundColor = LDGolbalColor.BgGoundGray //
        contentView?.bounces = false
        
        systemMessageBtn = UIButton.setDetailsBtn(titleArr[0], picName: nil, btnBackColor: nil, titleColor: LDFontColor.DeepGray)
        systemMessageBtn?.tag = btnsTag + 0
        systemMessageBtn?.bk_whenTapped({ [weak self] in
            self?.clickBtn((self?.systemMessageBtn)!)
            })
        
        
        feedBackBtn = UIButton.setDetailsBtn(titleArr[1], picName: nil, btnBackColor: nil, titleColor: LDFontColor.DeepGray)
        feedBackBtn?.tag = btnsTag + 1
        feedBackBtn?.bk_whenTapped({ [weak self] in
            self?.clickBtn((self?.feedBackBtn)!)
        })
        
        commentQuest = UIButton.setDetailsBtn(titleArr[2], picName: nil, btnBackColor: nil, titleColor: LDFontColor.DeepGray)
        commentQuest?.tag = btnsTag + 2
        commentQuest?.bk_whenTapped({ [weak self] in
            self?.clickBtn((self?.commentQuest)!)
            })
        
        
        line1 = UIView.init()
        line1?.backgroundColor = LDIconColor.LightGray
        line2 = UIView.init()
        line2?.backgroundColor = LDIconColor.LightGray
        
        view.addSubview(contentView!)
        view.addSubview(gampView!)
        
        gampView?.addSubview(systemMessageBtn!)
        gampView?.addSubview(feedBackBtn!)
        gampView?.addSubview(commentQuest!)
        gampView?.addSubview(line1!)
        gampView?.addSubview(line2!)
//        gampView?.backgroundColor = UIColor.redColor()
        gampView?.anchorAndFillEdge(.Top, xPad: 0, yPad: 0, otherSize: BtnH)
//        gampView?.snp_makeConstraints(closure: { (make) in
//            
//            make.top.equalTo(64)
//            make.left.right.equalTo(0)
//            make.height.equalTo(BtnH)
//        })
//        
//        systemMessageBtn?.snp_makeConstraints(closure: { (make) in
//            make.centerY.equalTo(gampView!)
//            make.size.equalTo(CGSizeMake(BtnW, BtnH))
//            make.left.equalTo(0)
//        })
        
        systemMessageBtn?.anchorToEdge(.Left, padding: 0, width: BtnW, height: BtnH)
        line1?.align(.ToTheRightCentered, relativeTo: systemMessageBtn!, padding: 0, width: 1, height: BtnH - LDMargin_V*4)
        feedBackBtn?.align(.ToTheRightCentered, relativeTo: line1!, padding: 0, width: BtnW, height: BtnH - LDMargin_V*4)
        line2?.align(.ToTheRightCentered, relativeTo: feedBackBtn!, padding: 0, width: 1, height: BtnH - LDMargin_V*4)
        commentQuest?.align(.ToTheRightCentered, relativeTo: line2!, padding: 0, width: BtnW, height: BtnH - LDMargin_V*4)
        
    }
    
    func setupContentVC() {
        
        //第一次选中
        clickBtn(feedBackBtn!)
        
    }
    
    func clickBtn(btn:UIButton) {
        
        changeBtnStatus(btn)
        let index = btn.tag - btnsTag
        
        if childVC[index] == nil {
            
            switch index {
            case 0:
                
                childVC[0] = systemMessageVC()
            case 1:
                
                childVC[1] = customerConversationVC
                
//                rac_signalForSelector(#selector(customerConversationVC.viewWillAppear(_:))).subscribeNext({
//                    [weak self] (obj) in
//                    
//                    self?.customerConversationVC.conversationMessageCollectionView.contentInset = UIEdgeInsetsMake(70, 0, 0, 0)
//                    
//                })
                
            case 2:
                
                childVC[2] = feedBackNormalQuesVC()
                
            
            default:
                break
            }
        }
        
        let vc = childVC[index] as! UIViewController
        title = titleArr[index]
        
        UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            [weak self] in
            self?.contentView?.contentOffset.x = CGFloat(index)*LDFrame.ScreenWidth//CGPointMake(CGFloat(index)*LDFrame.ScreenWidth, 65)
            
            }, completion: nil)
        
        if vc.view.superview != nil {
            return
        }
        vc.view.frame.origin.x = CGFloat(index)*LDFrame.ScreenWidth//CGRectMake(CGFloat(index)*LDFrame.ScreenWidth, 0, LDFrame.ScreenWidth, (contentView?.height)!)
        contentView?.addSubview(vc.view)//
    }
    
    func changeBtnStatus(btn:UIButton) {
        if btn == selectTitleButton {
            return
        }
        
        btn.setTitleColor(LDFontColor.Green, forState: .Normal)
        selectTitleButton.setTitleColor(LDFontColor.DeepGray, forState: .Normal)
        selectTitleButton = btn
    }
    
    //    deinit {
    //        let vc = childVC[1] as? meSettingFeedBackChat
    //        vc?.removeFromParentViewController()
    //    }
}
extension feedBackVC:UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let offIndex = (((contentView?.contentOffset.x)! % LDFrame.ScreenWidth) / LDFrame.ScreenWidth) > 0.5 ? 1:0
        let pageIndex = Int((contentView?.contentOffset.x)! / LDFrame.ScreenWidth) + offIndex
        
        
        let button = view.viewWithTag(btnsTag+pageIndex) as?UIButton
        //        debugPrint(button)
        
        if button != nil {
            clickBtn(button!)
        }
    }
    
}